package com.ywy.controller;

import com.ywy.entity.Res;
import com.ywy.entity.User;
import com.ywy.entity.UserDownload;
import com.ywy.service.ResService;
import com.ywy.service.UserDownloadService;
import com.ywy.service.UserService;
import com.ywy.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 资源下载Controller
 *
 * @author ywy
 * @date 2022/9/8 14:12
 */
@Controller
@RequestMapping("download")
public class DownloadController {
    @Resource
    private ResService resService;
    @Resource
    private UserDownloadService userDownloadService;
    @Resource
    private UserService userService;

    /**
     * 分页查询用户已下载资源信息
     * @param pageNo
     * @param session
     * @return
     */
    @RequestMapping("list")
    public ModelAndView list(@RequestParam(defaultValue = "1") Integer pageNo, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");

        // 分页查询用户已下载资源
        UserDownload userDownload = new UserDownload();
        userDownload.setUser(user);
        List<UserDownload> userDownloadList = userDownloadService.list(userDownload, pageNo, 10);
        Long total = userDownloadService.getTotal(userDownload);

        ModelAndView mav = new ModelAndView();
        mav.addObject("userDownloadList", userDownloadList);
        mav.addObject("pageCode", PageUtil.genPagination("/download/list", total, pageNo, 10, ""));
        mav.setViewName("user/download");
        return mav;
    }

    /**
     * 跳转到资源下载页面
     *
     * @param id
     * @param session
     * @return
     */
    @RequestMapping("toDownLoadPage/{id}")
    public ModelAndView toDownLoadPage(@PathVariable("id") Integer id, HttpSession session) {
        User currentUser = (User) session.getAttribute("currentUser");

        // 查询用户是否下载过该资源
        boolean isDownload;
        int count = userDownloadService.getCountByUserIdAndResId(currentUser.getId(), id);
        if (count > 0) {
            isDownload = true;
        } else {
            isDownload = false;
        }

        // 查询资源信息
        Res res = resService.getById(id);
        // 如果用户未下载过该资源
        if (!isDownload) {
            User user = userService.getById(currentUser.getId());
            // 判断用户积分是否足够
            if (user.getPoints() - res.getPoints() < 0) {
                return null;
            }

            // 扣除用户积分
            user.setPoints(user.getPoints() - res.getPoints());
            userService.save(user);

            // 给分享人加积分
            User resUser = res.getUser();
            resUser.setPoints(resUser.getPoints() + res.getPoints());
            userService.save(resUser);

            // 保存用户下载信息
            UserDownload userDownload = new UserDownload();
            userDownload.setRes(res);
            userDownload.setUser(user);
            userDownload.setDownloadDate(new Date());
            userDownloadService.save(userDownload);
        }

        ModelAndView mav = new ModelAndView();
        mav.addObject("res", res);
        mav.setViewName("res/download");
        return mav;
    }

    /**
     * 跳转到VIP资源下载页面
     *
     * @param id
     * @param session
     * @return
     */
    @RequestMapping("toVipDownLoadPage/{id}")
    public ModelAndView toVipDownLoadPage(@PathVariable("id") Integer id, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        // 判断是否是vip
        if (!user.isVip()) {
            return null;
        }

        // 查询用户是否下载过该资源
        boolean isDownload;
        int count = userDownloadService.getCountByUserIdAndResId(user.getId(), id);
        if (count > 0) {
            isDownload = true;
        } else {
            isDownload = false;
        }

        Res res = resService.getById(id);
        if (!isDownload) {
            // 保存用户下载信息
            UserDownload userDownload = new UserDownload();
            userDownload.setRes(res);
            userDownload.setUser(user);
            userDownload.setDownloadDate(new Date());
            userDownloadService.save(userDownload);
        }

        ModelAndView mav = new ModelAndView();
        mav.addObject("res", res);
        mav.setViewName("res/download");
        return mav;
    }

    /**
     * 判断资源是否下载过
     *
     * @param id
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("isDownload")
    public boolean isDownload(Integer id, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        int count = userDownloadService.getCountByUserIdAndResId(user.getId(), id);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }
}
